package com.kaikeba.controller;

import com.kaikeba.bean.BootStrapTableExpress;
import com.kaikeba.bean.Express;
import com.kaikeba.bean.Message;
import com.kaikeba.bean.ResultData;
import com.kaikeba.dao.BaseExpressDao;
import com.kaikeba.dao.impl.ExpressDaoMysql;
import com.kaikeba.mvc.ResponseBody;
import com.kaikeba.service.ExpressService;
import com.kaikeba.util.DateFormatUtil;
import com.kaikeba.util.JsonUtil;
import com.kaikeba.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: 平川
 * @Description:
 * @Date Created in 2021-07-30 20:21
 * @Modified By:
 */
public class ExpressController {



    @ResponseBody("/express/console.do")
    public String console(HttpServletRequest req, HttpServletResponse resp){
        List<Map<String, Integer>> data = ExpressService.console();

        Message message = new Message();
        if (data.size()==0){
            message.setStatus(-1);
        }else{
            message.setStatus(0);
        }
        System.out.println("进入console控制");
        message.setData(data);
        String json = JsonUtil.toJSON(message);
        return json;
    }

    @ResponseBody("/express/list.do")
    public String list(HttpServletRequest req, HttpServletResponse resp){
        //1.获取其实位置的索引
        int offset = Integer.parseInt(req.getParameter("offset"));
        //2.获取每页显示的个数
        int pageNumber = Integer.parseInt(req.getParameter("pageNumber"));
        //3.获取每页的集合
        List<Express> list = ExpressService.findAll(true, offset, pageNumber);
        //时间格式的转换
        ArrayList<BootStrapTableExpress> list2 = new ArrayList<>();
        for (Express e : list){
            String intime = DateFormatUtil.format(e.getIntime());
            String outtime = e.getOuttime()==null?"未出库":DateFormatUtil.format(e.getOuttime());
            String status = e.getStatus()==0?"待取件":"已取件";
            String code = e.getCode()==null?"已取件":e.getCode();
            BootStrapTableExpress e2 = new BootStrapTableExpress(e.getId(),e.getNumber(),e.getUsername(),e.getUserphone(),e.getCompany(),code,intime,outtime,status,e.getSysPhone());

            list2.add(e2);
        }
        //4.获取总共的条数
        List<Map<String, Integer>> console = ExpressService.console();
        int total = console.get(0).get("data1_size");
        //获取boostrap的对象
        ResultData<BootStrapTableExpress> data = new ResultData<>();
        data.setRows(list2);
        data.setTotal(total);
        String json = JsonUtil.toJSON(data);
        return json;
    }

    @ResponseBody("/express/insert.do")
    public String insert(HttpServletRequest req, HttpServletResponse resp){

        String number = req.getParameter("number");
        String company = req.getParameter("company");
        String username = req.getParameter("username");
        String userphone1 = req.getParameter("userphone1");
        Express express = new Express(number,username,userphone1,company, UserUtil.getUserPhone(req.getSession()));
        boolean flag = ExpressService.insert(express);
        Message message = new Message();
        if (flag){
            //录入成功！
            message.setStatus(0);
            message.setResult("快递录入成功！");
        }else {
            //录入失败！
            message.setStatus(-1);
            message.setResult("快递录入失败！");
        }
        String json = JsonUtil.toJSON(message);
        return json;
    }

    @ResponseBody("/express/find.do")
    public String find(HttpServletRequest req, HttpServletResponse resp){
        String number = req.getParameter("number");
        Express e = ExpressService.findByNumber(number);
        Message message = new Message();
        if (e==null){
            message.setStatus(-1);
            message.setResult("单号不存在！");
        }else{
            message.setStatus(0);
            message.setResult("查找成功");
            message.setData(e);
        }
        String json = JsonUtil.toJSON(message);
        return json;
    }
    
    @ResponseBody("/express/update.do")
    public String update(HttpServletRequest req, HttpServletResponse resp){
        int id = Integer.parseInt(req.getParameter("id"));
        String number = req.getParameter("number");
        String company = req.getParameter("company");
        String username = req.getParameter("username");
        String userphone = req.getParameter("userphone");
        int status = Integer.parseInt(req.getParameter("status"));
        Express newExpress = new Express();
        newExpress.setNumber(number);
        newExpress.setCompany(company);
        newExpress.setUsername(username);
        newExpress.setUserphone(userphone);
        newExpress.setStatus(status);
        boolean flag = ExpressService.update(id, newExpress);
        Message message = new Message();
        if (flag){
            message.setStatus(0);
            message.setResult("修改成功！");
        }else{
            message.setStatus(-1);
            message.setResult("修改失败！");
        }
        String json = JsonUtil.toJSON(message);
        return json;
    }

    @ResponseBody("/express/delete.do")
    public String delete(HttpServletRequest req, HttpServletResponse resp){
        int id = Integer.parseInt(req.getParameter("id"));
        boolean flag = ExpressService.delete(id);
        Message message = new Message();
        if (flag){
            message.setStatus(0);
            message.setResult("删除成功！");
        }else{
            message.setStatus(-1);
            message.setResult("删除失败！");
        }
        String json = JsonUtil.toJSON(message);
        return json;
    }
}
